package com.hot.hot_backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hot.hot_backend.pojo.HotContact;
import com.hot.hot_backend.pojo.HotMessage;
import com.hot.hot_backend.pojo.Vo;
import com.hot.hot_backend.service.impl.HotContactServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  联系我们
 * </p>
 *
 * @author hot
 * @since 2024-07-17
 */
@CrossOrigin
@RestController
@RequestMapping("/hot_contact")
public class HotContactController {

    @Resource
    private HotContactServiceImpl contactService;

    // 分页查询
    @GetMapping("/page/{current}")
    public Vo<HotContact> page(@PathVariable("current") Integer current) {
        Page<HotContact> contactPage = new Page<>(current <= 0 ? 1 : current, 6);
        IPage<HotContact> page = contactService.page(contactPage);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "当前并没有联系我们的！");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }

    // 搜索查询
    @GetMapping("/search/{current}/{name}")
    public Vo<HotContact> search(@PathVariable("current") Integer current, @PathVariable("name") String name) {
        LambdaQueryWrapper<HotContact> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(name),HotContact::getName, name);
        Page<HotContact> contactPage = new Page<>(current <= 0 ? 1 : current, 6);
        Page<HotContact> page = contactService.page(contactPage, wrapper);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "并没有该结果");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }

    // 删除
    @GetMapping("/delete/{id}")
    public Vo<HotContact> delete(@PathVariable("id") String id){
        UpdateWrapper<HotContact> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        boolean b = contactService.remove(wrapper);
        if (b){
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }

    // 修改回复状态
    @GetMapping("/update/{id}")
    public Vo<HotContact> update(@PathVariable("id") String id){
        UpdateWrapper<HotContact> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("satus","已读");
        boolean b = contactService.update(wrapper);
        if (b){
            return new Vo<>(b,"修改成功！");
        }else {
            return new Vo<>(b,"修改失败，请稍后重试");
        }
    }

    // 新增
    @PostMapping("/insert")
    public Vo<HotContact> insert(@RequestBody HotContact contact){
        contact.setSatus("未读");
        boolean b = contactService.save(contact);
        if (b){
            return new Vo<>(b,"新增成功！");
        }else {
            return new Vo<>(b,"新增失败，请稍后重试！");
        }
    }

}
